class CategoriesController < ApplicationController
  before_filter :login_required
  before_filter :question_pool_required
  
  def new
    @category = Category.new
    cats = session[:current_question_pool].descendants
    @categories = {}
    @default_parent_category_id = @categories['NONE'] = session[:current_question_pool].id
    cats.each do |cat|
      @categories[cat.name] = cat.id
    end
  end
  
  def create
    @category = Category.new params[:category]
    @category.creator_id = current_user
    if @category.save
      flash[:notice] = 'Category successfully added'
      redirect_to categories_path
    else
      render :action => :new
    end
  end
  
  def edit
    @category = Category.find params[:id]
    cats = session[:current_question_pool].descendants
    @categories = {}
    @categories['NONE'] = session[:current_question_pool].id 
    cats.each do |cat|
      @categories[cat.name] = cat.id
    end
    @selected_parent_category_id = @category.parent.id
  end
  
  def update
    @category = Category.find params[:id]
    if @category.update_attributes(params[:category])
      flash[:notice] = 'Category successfully updated'
      redirect_to category_path
    else
      render :action => :new
    end
  end

  def show
    @category = Category.find params[:id]
  end
  
  def index
    @root_category_id = session[:current_question_pool].id
    @all_categories   = session[:current_question_pool].descendants
    @focus_category   = @all_categories.first
    # select according to your choice...
    # this item will be selected node by default in the tree when it will first be loaded.
  end

  def display_clicked_item
    # this action will handle the two way syncronization...all the tree nodes(items) will be linked
    # to this action to show the detailed item on the left of the tree when the item is clicked
    # from the tree
    if request.xhr?
      @category = Category.find(params[:id]) rescue nil
      if @category
        # the code below will render all your RJS code inline and
        # u need not to have any .rjs file, isnt this interesting
        render :update do |page|
          page.hide "selected_item"
          page.replace_html "selected_item", :partial => "categories/category", :object => @category
          page.visual_effect 'toggle_appear', "selected_item"
        end
      else
        return render(:nothing => true)
      end
    end
  end

  def sort_ajax_tree
    if request.xhr?
      if @focus_category = Category.find(params[:id].split("_").first) rescue nil
        parent_category = Category.find params[:parent_id]
        render :update do |page|
          @focus_category.parent_id = parent_category.id
          @focus_category.save
          @all_categories = session[:current_question_pool].descendants
          page.replace_html "ajaxtree", :partial => "categories/ajaxtree", :locals => {:focus_category => @focus_category, :all_categories => @all_categories, :root_category_id => session[:current_question_pool].id}
          page.hide "selected_item"
          page.replace_html "selected_item", :partial => "categories/category", :object => @focus_category
          page.visual_effect 'toggle_appear', "selected_item"
        end
      else
        return render(:nothing => true)
      end
    end
  end
end